import requests
import json, cv2
from base64 import b64decode
import numpy as np
import logging

def result_post():
    url = "http://8.134.53.114:8277/visual/uavAiAlarm/msgUpload"
    json_data = {}
    """南水直播检测报警测试"""
    logging.info("|INFO: post data to {}|".format(url))
    ret = requests.post(url=url,json=json_data)
    status_code = ret.status_code
    if status_code != 200:
        logging.error("|ERR: code {}, result_post fail|".format(status_code))

def post_powerLine():
    json_data = {
        "id":0,
        "images":[
                # "http://36.138.254.186:888/ftp/ftp_test/0000008_02999_d_0000042.jpg", 
                # "http://36.138.254.186:888/ftp/vehicle/press_lane.jpg",
                "http://36.138.254.186:888/ftp/ftp_test/DJI_20230817145233_0104_Z.JPG",
                "http://36.138.254.186:888/ftp/ftp_test/9999951_00000_d_0000303.jpg",
                # "http://36.138.254.186:888/ftp/ftp_test/DJI_20230817145233_0104_Z.JPG",
                # "http://36.138.254.186:888/ftp/ftp_test/DJI_20230817145233_0104_Z.JPG",
                # "http://36.138.254.186:888/ftp/ftp_test/crack/DJI_20230217123043_0002_W.JPG",
                # "http://36.138.254.186:888/ftp/ftp_test/crack/img_20230220_112918.077.jpg",
                ],

        # "show_score":1,
    }
    url = "http://0.0.0.0:2682/powerLine_detect"

    request = requests.post(url=url,json = json_data)
    with open("data/output/temp_file.json","w") as f:
        jsondata = json.loads(request.text)
        json.dump(jsondata,f,ensure_ascii=False,indent=4)

    print(request)

def post_test():
    json_data = {
        "id":0,
        "images":[
                # "http://36.138.254.186:888/ftp/ftp_test/0000008_02999_d_0000042.jpg",
                # "http://36.138.254.186:888/ftp/ftp_test/0000008_02999_d_0000042.jpg",
                "http://36.138.254.186:888/ftp/ftp_test/img_20231120_164124.647.jpg",
                "http://36.138.254.186:888/ftp/ftp_test/9999951_00000_d_0000303.jpg",
                # "http://192.168.101.10:888/ftp/ftp_test/000047.jpg",
                # "http://192.168.101.10:888/ftp/ftp_test/000135.jpg",
                # "http://8.134.53.114:888/ftp/ftp_test/crack/DJI_20230217123043_0002_W.JPG",
                # "http://8.134.53.114:888/ftp/ftp_test/crack/img_20230220_112918.077.jpg",
                ],
        # "show_score":1,
    }
    url = "http://0.0.0.0:2682/obj_detect"
    # url = "http://0.0.0.0:2682/water_pollution_detect"

    request = requests.post(url=url,json = json_data)
    jsondata = json.loads(request.text)

    print(jsondata)

"http://8.134.53.114:888/ftp/building_change/train_a.png"

def post_test_building_change():
    json_data = {
        "id":0,
        "images":[
                ["http://8.134.53.114:888/ftp/building_change/train_a.png","http://8.134.53.114:888/ftp/building_change/train_b.png",]
                ],
    }
    url = "http://0.0.0.0:2682/building_change"

    request = requests.post(url=url,json = json_data)
    with open("data/output/temp_file.json","w") as f:
        jsondata = json.loads(request.text)
        json.dump(jsondata,f)

    print(request)

def rtsp_post():
    json_data = {
    "rtmp_push_path":"rtmp://218.94.155.86:1936/live/livestream_74_2",
    # "rtmp_push_path":"rtmp://218.94.155.86:1936/live/livestream_8_69439858747240f292120e6af0b6a28d",
    "rtsp_origin":"rtmp://218.94.155.86:1936/live/livestream_74_1",
    }
    # json_data = {
    #         "clientip":"192.168.1.22",
    #         "format": "json",
    #         "data":None,
    #         "results": None,
    #         "classify_ms":0,
    #         "wait_in_queue_ms":0
    #     }

    url = "http://0.0.0.0:2682/deepstream_rtsp_detect"

    request = requests.post(url=url,json = json_data)
    print(request)

def stop_rtmp():
    json_data = {'rtmp_push_path': "rtmp://218.94.155.86:1936/live/livestream_74_2"}
    url = "http://0.0.0.0:2682/stop_deepstream_rtsp_detect"

    request = requests.post(url=url,json = json_data)
    print(request)

def loop_test():
    json_data = {
    "rtmp_push_path":"rtmp://218.94.155.86:1936/live/livestream_74_2",
    # "rtmp_push_path":"rtmp://218.94.155.86:1936/live/livestream_8_69439858747240f292120e6af0b6a28d",
    "rtsp_origin":"rtmp://218.94.155.86:1936/live/livestream_74_1",
    }
    # json_data = {
    #         "clientip":"192.168.1.22",
    #         "format": "json",
    #         "data":None,
    #         "results": None,
    #         "classify_ms":0,
    #         "wait_in_queue_ms":0
    #     }

    url = "http://0.0.0.0:2682/api"
    request = requests.post(url=url,json = json_data)

if __name__ == "__main__":
    # post_powerLine()
    # post_test()
    # post_test_building_change()
    # result_post()
    import time
    for _ in range(100):
        rtsp_post()
        time.sleep(10)
        # loop_test()
        stop_rtmp()
        time.sleep(2)


